package com.phys2d.trashball;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;

import com.phys2d.trashball.objects.DrawableCircle;

import net.phys2d.raw.Body;
import net.phys2d.raw.World;

public class MouseEventHandler implements MouseMotionListener,MouseListener{

	
	private int x;
	private int y;
	private int width;
	private int height;
	
	private Point originForcePoint;
	private Point releaseForcePoint;
	
	private boolean calculatingForce = false;
	private boolean ballKicked = false;
	private World world;
	
	/*
	 * Pressed/Dragged/Release
	 */

	public MouseEventHandler(World world, int x, int y, int width, int height) {
		this.world = world;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.originForcePoint = null;
		this.releaseForcePoint = null;
	}


	public boolean isCalculatingForce() {
		return calculatingForce;
	}


	public void setCalculatingForce(boolean calculatingForce) {
		this.calculatingForce = calculatingForce;
	}


	public boolean isBallKicked() {
		return ballKicked;
	}


	public void setBallKicked(boolean ballKicked) {
		this.ballKicked = ballKicked;
	}


	private boolean isInForceArea(Point point) {
		return isInForceArea(point.x, point.y);
	}
	
	private boolean isInForceArea(int x, int y) {
		if(x >= this.x && x <= (this.width + this.x)) {
			if(y >= this.y && y <= (this.height + this.y)) {
				return true;
			}
		}
		return false;
	}
	
	public Point getOriginForcePoint() {
		return originForcePoint;
	}



	public void setOriginForcePoint(Point originForcePoint) {
		this.originForcePoint = originForcePoint;
	}



	public Point getReleaseForcePoint() {
		return releaseForcePoint;
	}



	public void setReleaseForcePoint(Point releaseForcePoint) {
		this.releaseForcePoint = releaseForcePoint;
	}



	public int getX() {
		return x;
	}


	public void setX(int x) {
		this.x = x;
	}


	public int getY() {
		return y;
	}


	public void setY(int y) {
		this.y = y;
	}


	public int getWidth() {
		return width;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
	}


	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent event) {
		if(isInForceArea(event.getPoint())) {
			ballKicked = false;
			calculatingForce = true;
			originForcePoint = event.getPoint();
		}
	}

	private double distance(Point p, Point q)
	{ 
		if(p == null || q == null)
			return 0;
		double dx   = p.x - q.x;         //horizontal difference 
		double dy   = p.y - q.y;         //vertical difference 
		double dist = Math.sqrt( dx*dx + dy*dy ); //distance using Pythagoras theorem
		return dist;
	}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(calculatingForce && distance(releaseForcePoint,originForcePoint) >= 5) {
			kickBall();
		}
	}
	
	private void kickBall() {
		try {
			calculatingForce = false;
			ballKicked = true;
			float forceX  =  (float) (originForcePoint.getX() - releaseForcePoint.getX());
			float forceY  = (float) (originForcePoint.getY() - releaseForcePoint.getY());
			if(forceX > 0) {
				DrawableCircle trashBall = new DrawableCircle(20);
				trashBall.setImage(Resources.getImage(Resources.Images.JABULANI));
				trashBall.setzOrder(-1);
				Body ball = new Body("TrashBall" + System.currentTimeMillis(), trashBall, 1);
				ball.setPosition((int)originForcePoint.getX(),(int)originForcePoint.getY());
				ball.setRotatable(true);
				ball.setRotation((float) Math.toRadians(65));
				ball.setRestitution(1);
				
				ball.setForce(forceX * 100, forceY * 100);
				
				world.add(ball);
			}

			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		releaseForcePoint = e.getPoint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

}
